﻿// For an introduction to the Navigation template, see the following documentation:
// http://go.microsoft.com/fwlink/?LinkId=232506
(function () {
    "use strict";

    WinJS.Binding.optimizeBindingReferences = true;

    var app = WinJS.Application;
    var activation = Windows.ApplicationModel.Activation;
    var nav = WinJS.Navigation;


    function onSettingsCommand(settingsCommand) {
        // The URI to launch
        var uriToLaunch = "http://www.tecmundo.com.br/empresas-e-instituicoes/283-politica-de-privacidade.htm";
        // Create a Uri object from a URI string 
        var uri = new Windows.Foundation.Uri(uriToLaunch);

        Windows.System.Launcher.launchUriAsync(uri).then(
           function (success) {
               if (success) {
                   // URI launched
               } else {
                   // URI launch failed
               }
           });
    }

    function onCommandsRequested(eventArgs) { /* Your code */
        var str = "e";
        var settingsCommand = new Windows.UI.ApplicationSettings.SettingsCommand("privacy", "Privacidade", onSettingsCommand);
        eventArgs.request.applicationCommands.append(settingsCommand);
    }

    app.addEventListener("activated", function (args) {
        if (args.detail.kind === activation.ActivationKind.launch) {
            if (args.detail.previousExecutionState !== activation.ApplicationExecutionState.terminated) {
                // TODO: This application has been newly launched. Initialize
                // your application here.

                var settingsPane = Windows.UI.ApplicationSettings.SettingsPane.getForCurrentView();
                settingsPane.addEventListener("commandsrequested", onCommandsRequested);

                var textBusca = document.getElementById('textBusca');
                textBusca.addEventListener('keydown', function (event) {
                    if (event.keyCode == 13) {
                        SearchProcess();
                    }
                }, false);

                var btn = document.getElementById('btnSearch');
                btn.addEventListener('click', SearchProcess);

            } else {
                // TODO: This application has been reactivated from suspension.
                // Restore application state here.
            }

            if (app.sessionState.history) {
                nav.history = app.sessionState.history;
            }

            //args.setPromise(WinJS.UI.processAll().done(function () {
            //    var btnSearch = document.getElementById("btnSearch");
            //    btnSearch.addEventListener("click", btnSearchClick, false);
            //}));

            args.setPromise(WinJS.UI.processAll().then(function () {
                if (nav.location) {
                    nav.history.current.initialPlaceholder = true;
                    return nav.navigate(nav.location, nav.state);
                } else {
                    return nav.navigate(Application.navigator.home);
                }
            }));
        }
    });

    app.oncheckpoint = function (args) {
        // TODO: This application is about to be suspended. Save any state
        // that needs to persist across suspensions here. If you need to 
        // complete an asynchronous operation before your application is 
        // suspended, call args.setPromise().
        WinJS.Application.sessionState.WSDataSource = null;
        WinJS.Application.sessionState.WSTopDataSource = null;
        app.sessionState.history = nav.history;

    };

    function SearchProcess() {
        nav.navigate("/pages/searchResult/searchResult.html", { searchText: document.getElementById("textBusca").value });
    }

    function btnSearchClick(mouseEvent) {
        
        var f = document.getElementById('textBusca');
        if (f.value != '' && f.value != null) {
            var listView = document.getElementById("listViewResult").winControl;
            listView.itemDataSource = WSData.SearchResult;
        }
        else 
            (new Windows.UI.Popups.MessageDialog("Informe um texto para pesquisa!").showAsync());
    }

    app.start();
})();
